﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// qt headers
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QColor>
#include <QIcon>

// custom headers
#include "CustomDrawer.h"
#include "GbmpNew.h"
#include "DebugMessage.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    CustomDrawer::CustomDrawer(QString &title, QString &subTitle, QWidget *parent) :
        QFrame(parent),
        m_pBtnHandle(nullptr),
        m_pLblTitle(nullptr),
        m_pLblSubTitle(nullptr),
        m_pHblAccessories(nullptr),
        m_pCentralLayout(nullptr),
        m_pCentralFrame(nullptr),
        m_title(title),
        m_subTitle(subTitle),
        m_headerBackground(245, 245, 245, 255),
        m_centralWidgetMargins(8, 8, 8, 8),
        m_drawnOutHandleIcon(":/images/icon_arrow_down_#596370_16_16.png"),
        m_drawnInHandleIcon(":/images/icon_arrow_right_#596370_16_16.png")
    {
        InitUI();
    }
    
    CustomDrawer::~CustomDrawer()
    {
    }

    void CustomDrawer::SetCentralWidget(QWidget * centralWidget)
    {
        if (!centralWidget)
        {
            return;
        }
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pCentralLayout, L"m_pCentralLayout为空", L"GMEP", L"2024-01-30");
        m_pCentralLayout->addWidget(centralWidget);
    }

    void CustomDrawer::AddAccessory(QWidget * accessory)
    {
        if (!accessory)
        {
            return;
        }
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHblAccessories, L"m_pHblAccessories为空", L"GMEP", L"2024-01-30");
        int32_t itemCount = m_pHblAccessories->count();
        for (int32_t i = 0; i < itemCount; ++i)
        {
            QLayoutItem  *pItem = m_pHblAccessories->itemAt(i);
            if (!pItem)
            {
                continue;
            }

            QWidget *pAccessory = qobject_cast<QWidget *>(pItem->widget());
            if (!pAccessory)
            {
                continue;
            }

            if (pAccessory == accessory)
            {
                return;
            }
        }

        m_pHblAccessories->addWidget(accessory);
    }

    void CustomDrawer::Draw(bool drawnIn)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pCentralFrame, L"m_pCentralFrame为空", L"GMEP", L"2024-01-30");
        m_pCentralFrame->setVisible(!drawnIn);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pBtnHandle, L"m_pBtnHandle为空", L"GMEP", L"2024-01-30");
        m_pBtnHandle->setIcon(QIcon(drawnIn ? m_drawnInHandleIcon : m_drawnOutHandleIcon));

        emit Drawn(drawnIn, m_pCentralFrame->height());
    }

    bool CustomDrawer::IsDrawnIn()
    {
        DBG_WARN_AND_RETURN_FALSE_UNLESS(m_pBtnHandle, L"m_pBtnHandle为空", L"GMEP", L"2024-01-30");
        return m_pBtnHandle->isChecked();
    }

    void CustomDrawer::InitUI()
    {
        // handle button
        m_pBtnHandle = NEW_AS_QT(QPushButton,QIcon(m_drawnOutHandleIcon), QString());
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pBtnHandle, L"m_pBtnHandle为空", L"GMEP", L"2024-01-30");
        m_pBtnHandle->setFixedSize(QSize(16, 16));
        m_pBtnHandle->setIconSize(QSize(12, 12));
        m_pBtnHandle->setObjectName("HandleButton");
        m_pBtnHandle->setCursor(QCursor(Qt::CursorShape::PointingHandCursor));
        setStyleSheet(
            "QPushButton#HandleButton{"
            "background:transparent;"
            "border:none"
            "}"
        );
        m_pBtnHandle->setCheckable(true);
        QObject::connect(m_pBtnHandle, &QPushButton::clicked, this, &CustomDrawer::Draw);

        // title label
        m_pLblTitle = NEW_AS_QT(QLabel,m_title);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pLblTitle, L"m_pLblTitle为空", L"GMEP", L"2024-01-30");
        m_pLblTitle->setObjectName("TitleLabel");
        m_pLblTitle->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        m_pLblTitle->setStyleSheet(
            "QLabel#TitleLabel{"
            "font-family:\"Microsoft YaHei\";"
            "font-size:12px;"
            "color:rgba(0,0,0,255);"
            "}"
        );

        // subTitle label
        m_pLblSubTitle = NEW_AS_QT(QLabel,m_subTitle);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pLblSubTitle, L"m_pLblSubTitle为空", L"GMEP", L"2024-01-30");
        m_pLblSubTitle->setObjectName("SubTitleLabel");
        m_pLblSubTitle->setStyleSheet(
            "QLabel#SubTitleLabel{"
            "font-family:\"Microsoft YaHei\";"
            "font-size:12px;"
            "color:rgba(120, 122, 131, 255);"
            "}"
        );

        // accessories
        m_pHblAccessories = NEW_AS_QT(QHBoxLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHblAccessories, L"m_pHblAccessories为空", L"GMEP", L"2024-01-30");
        m_pHblAccessories->setContentsMargins(8, 8, 8, 8);
        m_pHblAccessories->setSpacing(8);
        m_pHblAccessories->setAlignment(Qt::AlignRight);

        // header layout
        QHBoxLayout *pHeaderLayout = NEW_AS_QT(QHBoxLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pHeaderLayout, L"pHeaderLayout为空", L"GMEP", L"2024-01-30");
        pHeaderLayout->setContentsMargins(0, 0, 0, 0);
        pHeaderLayout->setSpacing(5);
        pHeaderLayout->addWidget(m_pBtnHandle);
        pHeaderLayout->addWidget(m_pLblTitle);
        pHeaderLayout->addWidget(m_pLblSubTitle);
        pHeaderLayout->addLayout(m_pHblAccessories);

        // header frame
        QFrame *pHeaderFrame = NEW_AS_QT_CHILD(QFrame, this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pHeaderFrame, L"pHeaderFrame为空", L"GMEP", L"2024-01-30");
        pHeaderFrame->setFixedHeight(30);
        pHeaderFrame->setObjectName("HeaderFramde");
        pHeaderFrame->setStyleSheet(
            QString(
                "QFrame#HeaderFramde{"
                "background:rgba(%1, %2, %3, %4);"
                "}")
            .arg(m_headerBackground.red())
            .arg(m_headerBackground.green())
            .arg(m_headerBackground.blue())
            .arg(m_headerBackground.alpha())
        );
        pHeaderFrame->setLayout(pHeaderLayout);
        pHeaderFrame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

        // splitting line
        QFrame *pSplittingLine = NEW_AS_QT_CHILD(QFrame, this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pSplittingLine, L"pSplittingLine为空", L"GMEP", L"2024-01-30");
        pSplittingLine->setContentsMargins(0, 0, 0, 0);
        pSplittingLine->setFrameStyle(QFrame::Shape::HLine | QFrame::Shadow::Plain);
        pSplittingLine->setFixedHeight(1);
        pSplittingLine->setObjectName("SplittingLine");
        pSplittingLine->setStyleSheet(
            "QFrame#SplittingLine{"
            "border:1px solid rgba(217, 221, 228, 255);"
            "}"
        );        

        // central widget
        m_pCentralLayout = NEW_AS_QT(QHBoxLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pCentralLayout, L"m_pCentralLayout为空", L"GMEP", L"2024-01-30");
        m_pCentralLayout->setContentsMargins(m_centralWidgetMargins);

        // central frame
        m_pCentralFrame = NEW_AS_QT_CHILD(QFrame, this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pCentralFrame, L"m_pCentralFrame为空", L"GMEP", L"2024-01-30");
        m_pCentralFrame->setLayout(m_pCentralLayout);

        // whole layout
        QVBoxLayout *pWholeLayout = NEW_AS_QT(QVBoxLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pWholeLayout, L"pWholeLayout为空", L"GMEP", L"2024-01-30");
        pWholeLayout->setContentsMargins(0, 0, 0, 0);
        pWholeLayout->setSpacing(0);
        pWholeLayout->setSizeConstraint(QLayout::SizeConstraint::SetMinimumSize);
        pWholeLayout->setAlignment(Qt::AlignTop);
        pWholeLayout->addWidget(pHeaderFrame);
        pWholeLayout->addWidget(pSplittingLine); 
        pWholeLayout->addWidget(m_pCentralFrame);

        // CustomDrawer self
        setLayout(pWholeLayout);
    }

}